package clientUtilGUI;

import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;

import clientMain.Login;
import clientMain.Main;
import clientView.SB_View;
import clientView.SC_View;
import clientView.SS_Calculate;
import clientView.SS_Condition;
import clientView.StatisticsSS;
import clientView.TS_Condition;

public class Tab_SSTS extends JToolBar implements ActionListener{
	private JButton btnComputeTS, btnComputeSalary, btnListSalary;  
	private JButton btnSalaryCoefficient, btnSalaryBonus;
	private JButton btnStatisticSalary;	

	public Tab_SSTS(){
		setFloatable(false);
		setOrientation(JToolBar.HORIZONTAL);
		setMargin(new Insets(0, 6, 0, 2));
		
		for(int i = 0; i<Login.listAuthorization.size(); i++){
			if(Login.listAuthorization.get(i).getModuleName().equals("TimeSheet")){
				btnComputeTS = new JButton(UtilityGUI.subMenuTS, new ImageIcon("src/image/Income-icon.png"));
				btnComputeTS.addActionListener(this);
				btnComputeTS.setVerticalTextPosition(JButton.BOTTOM);
				btnComputeTS.setHorizontalTextPosition(JButton.CENTER);
				add(btnComputeTS);
				break;
			}
		}

		for(int i = 0; i<Login.listAuthorization.size(); i++){
			if(Login.listAuthorization.get(i).getModuleName().equals("SalarySheet")){
				btnComputeSalary = new JButton(UtilityGUI.subMenuSSCal, new ImageIcon("src/image/Money-Calculator-icon.png"));
				btnComputeSalary.addActionListener(this);
				btnComputeSalary.setVerticalTextPosition(JButton.BOTTOM);
				btnComputeSalary.setHorizontalTextPosition(JButton.CENTER);
				add(btnComputeSalary);
				
				btnListSalary = new JButton(UtilityGUI.subMenuSSList, new ImageIcon("src/image/salary_icon copy.png"));
				btnListSalary.addActionListener(this);
				btnListSalary.setVerticalTextPosition(JButton.BOTTOM);
				btnListSalary.setHorizontalTextPosition(JButton.CENTER);
				add(btnListSalary);
				break;
			}
		}

		for(int i = 0; i<Login.listAuthorization.size(); i++){
			if(Login.listAuthorization.get(i).getModuleName().equals("SalaryCoefficient")){
				btnSalaryCoefficient = new JButton(UtilityGUI.subMenuSalaryCoefficent, new ImageIcon("src/image/money (1).png"));
				btnSalaryCoefficient.addActionListener(this);
				btnSalaryCoefficient.setVerticalTextPosition(JButton.BOTTOM);
				btnSalaryCoefficient.setHorizontalTextPosition(JButton.CENTER);
				add(btnSalaryCoefficient);
				break;
			}
		}

		for(int i = 0; i<Login.listAuthorization.size(); i++){
			if(Login.listAuthorization.get(i).getModuleName().equals("SalaryBonus")){
				btnSalaryBonus = new JButton(UtilityGUI.subMenuSalaryBonus, new ImageIcon("src/image/money.png"));
				btnSalaryBonus.addActionListener(this);
				btnSalaryBonus.setVerticalTextPosition(JButton.BOTTOM);
				btnSalaryBonus.setHorizontalTextPosition(JButton.CENTER);
				add(btnSalaryBonus);
				break;
			}
		}
		
		for(int i = 0; i<Login.listAuthorization.size(); i++){
			if(Login.listAuthorization.get(i).getModuleName().equals("TotalSalaryMonth") ||
					Login.listAuthorization.get(i).getModuleName().equals("TotalSalaryQuarter") ||
					Login.listAuthorization.get(i).getModuleName().equals("TotalSalaryYear")){
				btnStatisticSalary = new JButton(UtilityGUI.subMenuStatistic, new ImageIcon("src/image/chart-icon.png"));
				btnStatisticSalary.addActionListener(this);
				btnStatisticSalary.setVerticalTextPosition(JButton.BOTTOM);
				btnStatisticSalary.setHorizontalTextPosition(JButton.CENTER);
				add(btnStatisticSalary);
				break;
			}
		}

	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()== btnComputeTS){
			new TS_Condition(Main.main).setVisible(true);
		}
		if(e.getSource() == btnComputeSalary){
			int index = Main.jTabbedPane.positionTab(UtilityGUI.tabSSCal);
			if(index == -1){
				Main.jTabbedPane.addTab(new SS_Calculate(), UtilityGUI.tabSSCal);
				Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
			}else{
				Main.jTabbedPane.setSelectedIndex(index);
				return;
			}
		}
		if(e.getSource() == btnListSalary){
			new SS_Condition(Main.main).setVisible(true);
		}
		
		if(e.getSource() == btnSalaryCoefficient){
			int index = Main.jTabbedPane.positionTab(UtilityGUI.tabSC);
			if(index == -1){
				Main.jTabbedPane.addTab(new SC_View(), UtilityGUI.tabSC);
				Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
			}else{
				Main.jTabbedPane.setSelectedIndex(index);
				return;
			}
		}
		if(e.getSource() == btnSalaryBonus){
			int index = Main.jTabbedPane.positionTab(UtilityGUI.tabSB);
			if(index == -1){
				Main.jTabbedPane.addTab(new SB_View(), UtilityGUI.tabSB);
				Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
			}else{
				Main.jTabbedPane.setSelectedIndex(index);
				return;
			}
		}
		if(e.getSource() == btnStatisticSalary){
			int index = Main.jTabbedPane.positionTab(UtilityGUI.tabStatistic);
			if(index == -1){
				Main.jTabbedPane.addTab(new StatisticsSS(), UtilityGUI.tabStatistic);
				Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
			}else{
				Main.jTabbedPane.setSelectedIndex(index);
				return;
			}
		}
	}
}

